package com.example.controller;

import com.example.common.Result;
import com.example.common.PageResult;
import com.example.service.SupplyProductService;
import com.example.service.SupplyCompanyService;
import com.example.service.SupplyCategoryService;
import com.example.mapper.StoreInventoryMapper;
import com.example.entity.StoreInventory;
import com.example.util.UserContextUtil;
import com.example.vo.SupplyProductVO;
import com.example.vo.SupplyCompanyVO;
import com.example.vo.SupplyCategoryVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供货API控制器 - 专门用于库存管理
 */
@Slf4j
@RestController
@RequestMapping("/api/supply")
@RequiredArgsConstructor
public class SupplyController {
    
    private final SupplyProductService supplyProductService;
    private final SupplyCompanyService supplyCompanyService;
    private final SupplyCategoryService supplyCategoryService;
    private final StoreInventoryMapper storeInventoryMapper;
    private final UserContextUtil userContextUtil;
    
    /**
     * 分页查询供货商品（用于库存管理）
     */
    @GetMapping("/products")
    public Result<PageResult<SupplyProductVO>> getProductsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "false") Boolean withStoreStock) {
        try {
            log.info("查询供货商品 - 页码: {}, 大小: {}, 关键词: {}, 包含店长库存: {}", pageNum, pageSize, keyword, withStoreStock);
            Result<PageResult<SupplyProductVO>> result = supplyProductService.getSupplyProductPage(pageNum, pageSize, keyword);
            
            // 如果需要包含店长库存信息
            if (withStoreStock && result.getCode() == 200) {
                Long currentUserId = userContextUtil.getCurrentUserId();
                if (currentUserId != null) {
                    for (SupplyProductVO product : result.getData().getRecords()) {
                        StoreInventory storeInventory = storeInventoryMapper.selectByManagerAndProduct(currentUserId, product.getId());
                        // 将店长库存设置到stock字段中，用于前端显示
                        product.setStock(storeInventory != null ? storeInventory.getCurrentStock() : 0);
                    }
                }
            }
            
            log.info("查询结果 - 代码: {}, 数据条数: {}", result.getCode(), 
                    result.getData() != null ? result.getData().getRecords().size() : 0);
            return result;
        } catch (Exception e) {
            log.error("查询供货商品失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有供货公司
     */
    @GetMapping("/companies")
    public Result<List<SupplyCompanyVO>> getCompanies() {
        try {
            return supplyCompanyService.getAllEnabledSupplyCompanies();
        } catch (Exception e) {
            log.error("查询供货公司失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有供货分类
     */
    @GetMapping("/categories")
    public Result<List<SupplyCategoryVO>> getCategories() {
        try {
            return supplyCategoryService.getAllEnabledSupplyCategories();
        } catch (Exception e) {
            log.error("查询供货分类失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
} 